package com.mz.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class StringUtil {
	private static final char HEX_DIGITS[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
            'A', 'B', 'C', 'D', 'E', 'F'};
	public static boolean isBlank(String string) {
        return (string == null || string.trim().length() == 0);
    }
	
	public static String checkSetValueByNull(String defaultValue,String checkValue) {
		if(isBlank(checkValue)) return defaultValue.trim();
		return checkValue;
	}
	
	public static String toHexString(byte[] b) {
        StringBuilder sb = new StringBuilder(b.length * 2);
        for (byte b1 : b) {
            sb.append(HEX_DIGITS[(b1 & 0xf0) >>> 4]);
            sb.append(HEX_DIGITS[b1 & 0x0f]);
        }
        return sb.toString();
    }
	
	 public static String encryptToMD5(String string) {
	        StringBuilder sb = new StringBuilder();
	        try {
	            MessageDigest md = MessageDigest.getInstance("MD5");
	            byte[] hashValue = md.digest(string.getBytes());
	            for (byte b : hashValue) {
	                sb.append(Integer.toHexString((b & 0xf0) >> 4));
	                sb.append(Integer.toHexString(b & 0x0f));
	            }
	        } catch (NoSuchAlgorithmException e) {
	            e.printStackTrace();
	        }
	        return sb.toString();
	    }
}
